package com.example.servlet;

import com.example.Service.NewsService;
import com.example.pojo.News;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "GetAllNewsServlet", value = "/GetAllNewsServlet")
public class GetAllNewsServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String page =request.getParameter("page");
        int currentPage =0;
        if (page==null){
            currentPage =1;
        }else {
            currentPage =Integer.parseInt(page);
        }
        int pageSize =Integer.parseInt(request.getParameter("pageSize"));
        NewsService ns= new NewsService();
        List<News> list= ns.getAllNews(currentPage,pageSize);
        int total = ns.getTotal();
        Map<String,Object> m =new HashMap<>() ;
        m.put("code",200);
        m.put("msg","查询成功");
        m.put("total",total);//总行数
        m.put("data",list);
        Gson gson =new Gson();
        String json =gson.toJson(m);
        response.getWriter().append(json);
    }

    public void destroy() {
    }
}